package com.zlq.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlq.reggie.common.CustomException;
import com.zlq.reggie.entity.Category;
import com.zlq.reggie.entity.Dish;
import com.zlq.reggie.entity.Setmeal;
import com.zlq.reggie.mapper.CategoryMapper;
import com.zlq.reggie.service.CategorySercice;
import com.zlq.reggie.service.DishService;
import com.zlq.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategorySercice{

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long ids) {
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper();
        dishQueryWrapper.eq(Dish::getCategoryId,ids);
        int dishCount = dishService.count(dishQueryWrapper);
        //查询当前分类是否关联了菜品，如果关联，抛出异常
        if (dishCount > 0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper();
        setmealQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int setmealCount = setmealService.count(setmealQueryWrapper);
        //是否关联套餐
        if (setmealCount > 0 ){
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }
        super.removeById(ids);

    }
}
